﻿package Control;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import Model.GioHang;
import Model.HoaDon;
import Model.InfoPayment;
import Model.ThongTinNguoiNhan;
import Model.ThongTinUser;
import Model.User;

/**
 * Servlet implementation class SaveOrder
 */
public class SaveOrder extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveOrder() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
       HttpSession session = request.getSession();
//       InfoPayment pay = (InfoPayment) session.getAttribute("pay");
       ThongTinNguoiNhan pay = (ThongTinNguoiNhan) session.getAttribute("pay");
       GioHang cart = (GioHang) session.getAttribute("cart");
       User user = (User) session.getAttribute("sessionlogin");
       if(pay!=null){
           String delivery="";
          if (pay.getThongTinUser().getTinh() == 1) {
                                        delivery= "Giao hàng tận nơi";
                                    } else {
                                        delivery="Giao hàng qua bưu điện";}
      //     OrderToCustomer order = new OrderToCustomer(user.getUserID(), null, "Chưa giao hàng", pay.getName(),Integer.parseInt(pay.getProvinceID()) ,pay.getAddress() , pay.getPhone(), cart.getSum(),pay.getIDCARD(), pay.getNote(), pay.getPayType(), delivery, cart) ;
       //    order.saveOrder();
           java.util.Date today=new java.util.Date();
           java.sql.Date todaysql= new Date(today.getTime());
           HoaDon hoadon = new HoaDon(user.getUserID(),cart,todaysql,null,pay,"Chưa giao hàng");
	hoadon.saveOrder();
       //    ThongTinUser thongTinUser= new ThongTinUser(us, dienThoai, tinh, diaChi);
       //    ThongTinNguoiNhan thongTinNguoiNhan = new ThongTinNguoiNhan(cMND, ghiChu, thongTinUser);
           
           
           
           response.sendRedirect("index.jsp");
       }
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
